package com.ac.api.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.context.Context;
import com.base.context.ContextHolder;
import com.ac.api.component.dao.CpAttachmentMapper;
import com.ac.api.component.dao.CpLabelMapper;
import com.ac.api.component.entity.CpAttachment;
import com.ac.api.component.service.CpAttachmentService;
import com.ac.api.component.vo.CpAttachmentVo;
import com.ac.api.component.vo.CpLabelVo;
import com.ac.utils.FileUtils;
import com.ac.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-17
 */
@Service
public class CpAttachmentServiceImpl extends ServiceImpl<CpAttachmentMapper, CpAttachment> implements CpAttachmentService {
    @Autowired
    private CpLabelMapper cpLabelMapper;

    @Override
    public CpAttachmentVo updateAttachment(CpAttachmentVo cpAttachmentVo) {
        Context context = ContextHolder.currentContext();
        //查找对应模块名称和标签名称的标签
        CpLabelVo cpLabelVo=cpLabelMapper.findByName(cpAttachmentVo.getModuleName(),cpAttachmentVo.getLabelName());
        if(cpLabelVo==null){
            return null;
        }
        cpAttachmentVo.setUserid(context.currentUser().getId());
        cpAttachmentVo.setCpLabelId(cpLabelVo.getLabelId());
        cpAttachmentVo.setState("t");
        cpAttachmentVo.setSize(FileUtils.getFileLength(cpAttachmentVo.getPath())+"");
        this.updateById(cpAttachmentVo);
        return cpAttachmentVo;
    }

    @Override
    public CpAttachmentVo saveAttachment(CpAttachmentVo cpAttachmentVo) {
        Context context = ContextHolder.currentContext();
        //查找对应模块名称和标签名称的标签
        CpLabelVo cpLabelVo=cpLabelMapper.findByName(cpAttachmentVo.getModuleName(),cpAttachmentVo.getLabelName());
        if(cpLabelVo==null){
            return null;
        }
        cpAttachmentVo.setUserid(context.currentUser().getId());
        cpAttachmentVo.setCreatetime(new Date());
        cpAttachmentVo.setCpLabelId(cpLabelVo.getLabelId());
        cpAttachmentVo.setState("t");
        cpAttachmentVo.setSize(FileUtils.getFileLength(cpAttachmentVo.getPath())+"");
        this.save(cpAttachmentVo);
        return  cpAttachmentVo;
    }

    @Override
    public List<CpAttachment> list(CpAttachment example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<CpAttachment> page(CpAttachment example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public CpAttachment findByCode(String code) {
        return this.baseMapper.findByCode(code);
    }

    @Override
    public List<CpAttachment> findByIds(List<Integer> ids) {
        QueryWrapper<CpAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CpAttachment::getId, ids)
                .orderByDesc(CpAttachment::getId);
        return super.list(wrapper);
    }

    @Override
    public Boolean removeByIds(List<Integer> ids) {
        QueryWrapper<CpAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CpAttachment::getId, ids);
        return super.remove(wrapper);
    }

    private QueryWrapper<CpAttachment> buildWrapper(CpAttachment example) {
        QueryWrapper<CpAttachment> wrapper = new QueryWrapper<>(example);
        wrapper.lambda().orderByDesc(CpAttachment::getId);
        return wrapper;
    }

    @Override
    public Page<CpAttachmentVo> findByPage(CpAttachmentVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(this.baseMapper.findByExample(buildCpAttachmentVoWrapper(example), page));
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<CpAttachmentVo> buildCpAttachmentVoWrapper(CpAttachmentVo example) {
        QueryWrapper<CpAttachmentVo> wrapper     = new QueryWrapper<>();
        wrapper.like(StringUtil.isNotBlank(example.getName()),"name",example.getName())
                .like(StringUtil.isNotBlank(example.getLabelName()),"label_name",example.getLabelName())
                .like(StringUtil.isNotBlank(example.getModuleName()),"module_name",example.getModuleName())
                .eq(example.getId()!=null,"id",example.getId())
                .eq("state","t")
                .orderByDesc("createtime");
        return wrapper;
    }

}
